package com.fyk.controller;

import com.fyk.entity.Employee;
import com.fyk.service.EmpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author 范宇坤
 * date: 2021/8/4 14:05<br/>
 */
@Controller
@RequestMapping("emp")
public class EmpController {

    private static final Logger log = LoggerFactory.getLogger(EmpController.class);
    private EmpService empService;
    @Autowired
    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }

    //修改员工信息
    @RequestMapping("empUpdate")
    public String empUpdate(String empNum,Model model){
        log.info("修改员工的编号: {}",empNum);
        //根据工号查询所有信息返回到修改页面展示
        Employee dbEmployee = empService.queryEmpByNum(empNum);
        model.addAttribute("dbEmployee",dbEmployee);
        return "empUpdate";
    }

    //删除员工
    @RequestMapping("empDelete")
    public String empDelete(String empNum){
        log.info("删除员工的编号: {}",empNum);
        empService.deleteEmpByName(empNum);
        return "redirect:/emp/empList";
    }

    //添加员工
    @RequestMapping("empAdd")
    public String empAdd(Employee employee, MultipartFile photo){
        log.info("注册员工 姓名: {},性别: {},工号: {},薪水: {},生日: {},地址: {},职位: {},部门: {},电话: {}",
                employee.getEmpName(),employee.getEmpSex(),employee.getEmpNum(),employee.getEmpSalary(),
                employee.getEmpBirthday(),employee.getEmpAddress(),employee.getEmpJob(),employee.getEmpDep(),employee.getEmpPhone());
        log.info("上传证件照名称: {},证件照大小: {}",photo.getOriginalFilename(),photo.getSize());
        //照片暂时先不加
        employee.setEmpPhoto("xxx");
        //首先需要判断该员工是否存在
        Employee dbEmployee = empService.queryEmpByNum(employee.getEmpNum());
        if(ObjectUtils.isEmpty(dbEmployee)){
            empService.addEmp(employee);
        }
        return "redirect:/emp/empList";
    }

    //查询所有员工然后返回给empList页面
    @RequestMapping("empList")
    public String empList(Model model){
        List<Employee> employees = empService.queryAllEmp();
        model.addAttribute("employees",employees);
        return "empList";
    }
}
